package fr.utbm.to52.android_bibtex.model.spec;

import my.javax.swing.event.EventListenerList;
import my.util.MyObjects;

public class FieldSpecification {

	private final String name;

	private ValueType type;
	public static final String TYPE_PROPERTY_NAME = "type";

	private final EventListenerList listeners = new EventListenerList();

	public FieldSpecification(String name) {
		assert name!=null && !name.isEmpty();

		this.name = name;
	}

	public FieldSpecification(String name, ValueType type) {
		assert name!=null && !name.isEmpty();
		assert type!=null;

		this.name = name;
		this.type = type;
	}

	public String getName() {
		return this.name;
	}

	public ValueType getType() {
		return this.type;
	}

	public void setType(ValueType type) {
		assert type!=null;

		if (!MyObjects.equals(this.type, type)) {
			ValueType oldType = this.type;
			this.type = type;
			fireChange(TYPE_PROPERTY_NAME, oldType, this.type);
		}
	}

	public void addFieldSpecificationListener(FieldSpecificationListener l) {
		assert l!=null;

		this.listeners.add(FieldSpecificationListener.class, l);
	}

	public void removeFieldSpecificationListener(FieldSpecificationListener l) {
		assert l!=null;

		this.listeners.remove(FieldSpecificationListener.class, l);
	}

	public FieldSpecificationListener[] getFieldSpecificationListeners() {
		return this.listeners.getListeners(FieldSpecificationListener.class);
	}

	protected void fireChange(String propertyName, Object oldValue, Object newValue) {
		FieldSpecificationEvent event = new FieldSpecificationEvent(this, propertyName, oldValue, newValue);
		for (FieldSpecificationListener listener : getFieldSpecificationListeners()) {
			listener.fieldSpecificationChanged(event);
		}
	}

	@Override
	public String toString() {
		return getName();
	}

}
